package com.zqxx.examine.management.controller.page.teacher;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.common.utils.QuestionAdapter;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.ExamPaperService;
import com.zqxx.examine.management.service.ExamService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.exam.Exam;
import com.zqxx.examine.model.exam.ExamHistory;
import com.zqxx.examine.model.exam.ExamPaper;
import com.zqxx.examine.model.question.QuestionQueryResult;
import com.zqxx.examine.model.user.Group;

@Controller
public class ExamPageTeacher
{
  @Autowired
  private UserService userService;
  @Autowired
  private ExamPaperService examPaperService;
  @Autowired
  private ExamService examService;
  
  @RequestMapping(value={"/teacher/exam/exam-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examListPage(Model model, HttpServletRequest request, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<Exam> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(8);
    List<Exam> examList = this.examService.getExamList(pageModel, new int[] { 1, 2 });
    String pageStr = PagingUtil.getPagelink(page, pageModel.getTotalPage(), "", "teacher/exam/exam-list");
    
    model.addAttribute("examList", examList);
    model.addAttribute("pageStr", pageStr);
    return "exam-list";
  }
  
  @RequestMapping(value={"/teacher/exam/model-test-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String modelTestListPage(Model model, HttpServletRequest request, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<Exam> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(10);
    List<Exam> examList = this.examService.getExamList(pageModel, new int[] { 3 });
    String pageStr = PagingUtil.getPageBtnlink(page, pageModel
      .getTotalPage());
    model.addAttribute("examList", examList);
    model.addAttribute("pageStr", pageStr);
    return "model-test-list";
  }
  
  @RequestMapping(value={"/teacher/exam/exam-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examAddPage(Model model, HttpServletRequest request)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    List<Group> groupList = this.userService.getGroupListByUserId(userInfo.getUserid(), null);
    List<ExamPaper> examPaperList = this.examPaperService.getEnabledExamPaperList(userInfo.getUsername(), null);
    model.addAttribute("groupList", groupList);
    model.addAttribute("examPaperList", examPaperList);
    return "exam-add";
  }
  
  @RequestMapping(value={"/teacher/exam/model-test-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String modelTestAddPage(Model model, HttpServletRequest request)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    List<ExamPaper> examPaperList = this.examPaperService.getEnabledExamPaperList(userInfo.getUsername(), null);
    
    model.addAttribute("examPaperList", examPaperList);
    return "model-test-add";
  }
  
  @RequestMapping(value={"/teacher/exam/exam-student-list/{examId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examStudentListPage(Model model, HttpServletRequest request, @PathVariable int examId, @RequestParam(value="searchStr", required=false, defaultValue="") String searchStr, @RequestParam(value="order", required=false, defaultValue="") String order, @RequestParam(value="limit", required=false, defaultValue="0") int limit, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Page<ExamHistory> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(10);
    if ("".equals(searchStr)) {
      searchStr = null;
    }
    if (("".equals(order)) || ((!"desc".equals(order)) && (!"asc".equals(order)))) {
      order = null;
    }
    List<ExamHistory> histList = this.examService.getUserExamHistListByExamId(examId, searchStr, order, limit, pageModel);
    String pageStr = PagingUtil.getPagelink(page, pageModel
      .getTotalPage(), "", "teacher/exam/exam-student-list/" + examId);
    List<Group> groupList = this.userService.getGroupListByUserId(userInfo.getUserid(), null);
    model.addAttribute("groupList", groupList);
    model.addAttribute("histList", histList);
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("examId", Integer.valueOf(examId));
    model.addAttribute("limit", Integer.valueOf(limit));
    model.addAttribute("order", order);
    model.addAttribute("searchStr", searchStr);
    return "user-exam-list";
  }
  
  @RequestMapping(value={"/teacher/exam/student-answer-sheet/{histId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String studentAnswerSheetPage(Model model, HttpServletRequest request, @PathVariable int histId)
  {
    ExamHistory history = this.examService.getUserExamHistListByHistId(histId);
    int examPaperId = history.getExamPaperId();
    

    String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/";
    
    ExamPaper examPaper = this.examPaperService.getExamPaperById(examPaperId);
    StringBuilder sb = new StringBuilder();
    if ((examPaper.getContent() != null) && (!examPaper.getContent().equals("")))
    {
      Gson gson = new Gson();
      String content = examPaper.getContent();
      List<QuestionQueryResult> questionList = (List)gson.fromJson(content, new TypeToken() {}.getType());
      for (QuestionQueryResult question : questionList)
      {
        QuestionAdapter adapter = new QuestionAdapter(question, strUrl);
        sb.append(adapter.getStringFromXML());
      }
    }
    model.addAttribute("htmlStr", sb);
    model.addAttribute("exampaperid", Integer.valueOf(examPaperId));
    model.addAttribute("examHistoryId", Integer.valueOf(history.getHistId()));
    model.addAttribute("exampapername", examPaper.getName());
    model.addAttribute("examId", Integer.valueOf(history.getExamId()));
    return "student-answer-sheet";
  }
  
  @RequestMapping(value={"/teacher/exam/mark-exampaper/{examhistoryId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String markExampaperPage(Model model, HttpServletRequest request, @PathVariable int examhistoryId)
  {
    ExamHistory history = this.examService.getUserExamHistListByHistId(examhistoryId);
    int examPaperId = history.getExamPaperId();
    

    String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/";
    
    ExamPaper examPaper = this.examPaperService.getExamPaperById(examPaperId);
    StringBuilder sb = new StringBuilder();
    if ((examPaper.getContent() != null) && (!examPaper.getContent().equals("")))
    {
      Gson gson = new Gson();
      String content = examPaper.getContent();
      List<QuestionQueryResult> questionList = (List)gson.fromJson(content, new TypeToken() {}.getType());
      for (QuestionQueryResult question : questionList)
      {
        QuestionAdapter adapter = new QuestionAdapter(question, strUrl);
        sb.append(adapter.getStringFromXML());
      }
    }
    model.addAttribute("htmlStr", sb);
    model.addAttribute("exampaperid", Integer.valueOf(examPaperId));
    model.addAttribute("examHistoryId", Integer.valueOf(history.getHistId()));
    model.addAttribute("exampapername", examPaper.getName());
    model.addAttribute("examId", Integer.valueOf(history.getExamId()));
    return "exampaper-mark";
  }
}